package cn.imvc.app.controllers.member;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.VoucherTemplate;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.VoucherTemplateModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

/**
 * 
 * @author wujx
 * @time 2016年9月8日
 *
 */
public class VoucherTemplateControllerImpl implements VoucherTemplateController {
	static Logger logger = Logger.getLogger(VoucherTemplateControllerImpl.class);
	public Page<VoucherTemplate> page;
	public ViewRequest viewRequest;
	private int id;
	@Autowired
	private VoucherTemplateModel voucherTemplateModel;
	private VoucherTemplate voucherTemplate;
	private Double theLimitMoney;
	private Double thePrice;
	
	@Autowired
	LoggingModel loggingModel;
	
	@Override
	public String search() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String index() {
		page = voucherTemplateModel.findAll(viewRequest);
		return "voucher-template/index";
	}
	@Override
	public String show() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String editNew() {
		return "voucher-template/edit";
	}
	@Override
	public String create() {
		voucherTemplate.setPrice((int)(thePrice*100));
		voucherTemplate.setLimitMoney((int)(theLimitMoney*100));
		voucherTemplateModel.save(voucherTemplate);
		loggingModel.info(logger,"创建代金券："+voucherTemplate.getId());
		return Render.json(new DialogBean("200", "创建成功","","closeCurrent"));
	}
	@Override
	public String edit() {
		voucherTemplate = voucherTemplateModel.getOne(id);
		return "voucher-template/edit";
	}
	@Override
	public String update() {
		voucherTemplate.setPrice((int)(thePrice*100));
		voucherTemplate.setLimitMoney((int)(theLimitMoney*100));
		voucherTemplateModel.update(voucherTemplate);
		loggingModel.info(logger,"编辑代金券："+voucherTemplate.getId());
		return Render.json(new DialogBean("200", "更新成功","","closeCurrent"));
	}
	@Override
	public String destroy() {
		String result = voucherTemplateModel.delete(id);
		loggingModel.info(logger,"删除代金券："+voucherTemplate.getId());
		if(result.equals("")){
			return Render.json(new DialogBean("200","删除成功",""));
		}else{
			return Render.json(new DialogBean("300", result,"")); 
		}
	}
	@Override
	public String destroyBatch() {
		// TODO Auto-generated method stub
		return null;
	}
	public VoucherTemplate getVoucherTemplate() {
		return voucherTemplate;
	}
	public void setVoucherTemplate(VoucherTemplate voucherTemplate) {
		this.voucherTemplate = voucherTemplate;
	}
	public Page<VoucherTemplate> getPage() {
		return page;
	}
	public void setPage(Page<VoucherTemplate> page) {
		this.page = page;
	}
	public ViewRequest getViewRequest() {
		return viewRequest;
	}
	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Double getTheLimitMoney() {
		return theLimitMoney;
	}
	public void setTheLimitMoney(Double theLimitMoney) {
		this.theLimitMoney = theLimitMoney;
	}
	public Double getThePrice() {
		return thePrice;
	}
	public void setThePrice(Double thePrice) {
		this.thePrice = thePrice;
	}

}
